package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.PictureAddNewDTO;
import cn.tedu.csmall.product.service.IPictureService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 处理图片的控制器
 * @author 何惠民
 */
@Validated
@Api(tags = "06图片管理模块")
@Slf4j
@RestController
@RequestMapping( "/pictures")
public class PictureController {
    @Autowired
    IPictureService pictureService;

    public PictureController() {
        log.debug("创建控制器:PictureController");
    }

    @ApiOperation("添加图片")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNewAlbum(@Valid PictureAddNewDTO pictureAddNewDTO){
        log.debug("开始处理[添加图片]的请求,参数{}",pictureAddNewDTO);
        pictureService.addNew(pictureAddNewDTO);
        log.debug("添加图片数据成功");
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除图片")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "图片id", required = true ,dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@Range(min = 1, message = "删除图片失败,删除的图片ID无效")
                                       @PathVariable Long id){
        log.debug("开始处理[删除图片]的请求,参数:{}",id);
        pictureService.deleteById(id);
        log.debug("删除Id为{}的图片成功",id);
        return JsonResult.ok();
    }

}
